#include "SSE.h"

SSE &SSE::engine()
{
	static SSE instance;
	return instance;
}

void SSE::start(int width, int height, int bpp, char * title, bool fullscreen)
{
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		stop();
	
	Uint32 flags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
	if(fullscreen)
		flags += SDL_FULLSCREEN;
	
	if((screen = SDL_SetVideoMode(width, height, bpp, flags)) == NULL)
		stop();
	
	SDL_WM_SetCaption(title, NULL);
	
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, bpp);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bpp);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	
	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	glOrtho(0, width, height, 0, -1, 1);
	
	glMatrixMode(GL_MODELVIEW);
}

void SSE::stop()
{
	SDL_Quit();
	exit(0);
}

void SSE::flip()
{
	SDL_GL_SwapBuffers();
}